package controller

import (
	"errors"
	"github.com/labstack/echo"
	"github.com/sirupsen/logrus"
	"net/http"
	"src/config"
	"src/model"
	"src/util/context"
	"src/util/log"
	"strconv"
)

var LostPropertyLogger = log.GetLogger()

func writeLostPropertyLoggerLoggerLog(funcName, errMsg string, err error) {
	LostPropertyLogger.WithFields(logrus.Fields{
		"package":  config.LogMessage,
		"file":     "LostProperty.go",
		"function": funcName,
		"err":      err,
	}).Warn(errMsg)
}

func CreatLostProperty(c echo.Context) error {
	c.Response().Header().Set("Access-Control-Allow-Origin", "*")
	c.Response().Header().Set("Access-Control-Allow-Methods", "GET,POST")
	c.Response().Header().Set("headers", "Content-Length, Access-Control-Allow-Origin, Access-Control-Allow-Headers, Cache-Control, Content-Language, Content-Type")
	c.Response().Header().Set("Access-Control-Allow-Credentials", "true")
	c.Response().Header().Set("Access-Control-Expose-Header", "Origin, X-Requested-With, Content-Type, Accept, Authorization")

	LostProperty := CreateLostPropertyParam{}
	_ = c.Bind(&LostProperty)
	if LostProperty.Title == "" {
		writeLostPropertyLoggerLoggerLog("CreatLostProperty", "Content为空", errors.New("Content为空"))
		return context.RetError(c, http.StatusBadRequest, http.StatusBadRequest, "Content为空")
	}
	newLostProperty, err := model.CreatLostProperty(LostProperty.Title, LostProperty.Remarks, LostProperty.Method, LostProperty.Time, LostProperty.Place, LostProperty.Imgs)
	if err != nil {
		writeLostPropertyLoggerLoggerLog("CreatLostProperty", err.Error(), err)
		return context.RetError(c, http.StatusBadRequest, http.StatusBadRequest, err.Error())
	}
	return context.RetData(c, newLostProperty)
}

//更改失物信息
func ChangeLostProperty(c echo.Context) error {
	c.Response().Header().Set("Access-Control-Allow-Origin", "*")
	c.Response().Header().Set("Access-Control-Allow-Methods", "GET,POST")
	c.Response().Header().Set("headers", "Content-Length, Access-Control-Allow-Origin, Access-Control-Allow-Headers, Cache-Control, Content-Language, Content-Type")
	c.Response().Header().Set("Access-Control-Allow-Credentials", "true")
	c.Response().Header().Set("Access-Control-Expose-Header", "Origin, X-Requested-With, Content-Type, Accept, Authorization")

	LostProperty := CreateLostPropertyParam{}
	_ = c.Bind(&LostProperty)
	if LostProperty.Id == "" {
		writeLostPropertyLoggerLoggerLog("ChangeLostProperty", "id为空", errors.New("id为空"))
		return context.RetError(c, http.StatusBadRequest, http.StatusBadRequest, "id为空")
	}
	newLostProperty := model.LostProperty{}
	newLostProperty.Place = LostProperty.Place
	newLostProperty.Time = LostProperty.Time
	newLostProperty.Method = LostProperty.Method
	newLostProperty.Imgs = LostProperty.Imgs
	newLostProperty.Remarks = LostProperty.Remarks
	newLostProperty.Title = LostProperty.Title
	err := model.ChangeLostProperty(LostProperty.Id, newLostProperty)
	if err != nil {
		writeLostPropertyLoggerLoggerLog("ChangeLostProperty", err.Error(), err)
		return context.RetError(c, http.StatusBadRequest, http.StatusBadRequest, err.Error())
	}
	return context.RetData(c, "")
}

func FinishLostProperty(c echo.Context) error {
	c.Response().Header().Set("Access-Control-Allow-Origin", "*")
	c.Response().Header().Set("Access-Control-Allow-Methods", "GET,POST")
	c.Response().Header().Set("headers", "Content-Length, Access-Control-Allow-Origin, Access-Control-Allow-Headers, Cache-Control, Content-Language, Content-Type")
	c.Response().Header().Set("Access-Control-Allow-Credentials", "true")
	c.Response().Header().Set("Access-Control-Expose-Header", "Origin, X-Requested-With, Content-Type, Accept, Authorization")

	LostProperty := LostPropertyFinishs{}
	_ = c.Bind(&LostProperty)
	if len(LostProperty.Finishers) == 0 {
		writeLostPropertyLoggerLoggerLog("FinishLostProperty", "为空", errors.New("为空"))
		return context.RetError(c, http.StatusBadRequest, http.StatusBadRequest, "为空")
	}
	ResData := LostPropertyResDataParam{}
	for _, j := range LostProperty.Finishers {
		err := model.FinishLostProperty(j.Id, j.Finisher)
		if err != nil {
			writeLostPropertyLoggerLoggerLog("FinishLostProperty", err.Error(), err)
			ResData.Failed = append(ResData.Failed, j.Id)
			ResData.Reason = append(ResData.Reason, err.Error())
		} else {
			ResData.Success = append(ResData.Success, j.Id)
		}
	}
	return context.RetData(c, ResData)
}

func DeleteLostProperty(c echo.Context) error {
	c.Response().Header().Set("Access-Control-Allow-Origin", "*")
	c.Response().Header().Set("Access-Control-Allow-Methods", "GET,POST")
	c.Response().Header().Set("headers", "Content-Length, Access-Control-Allow-Origin, Access-Control-Allow-Headers, Cache-Control, Content-Language, Content-Type")
	c.Response().Header().Set("Access-Control-Allow-Credentials", "true")
	c.Response().Header().Set("Access-Control-Expose-Header", "Origin, X-Requested-With, Content-Type, Accept, Authorization")

	LostProperty := IDsparam{}
	_ = c.Bind(&LostProperty)
	if len(LostProperty.Id) == 0 {
		writeLostPropertyLoggerLoggerLog("DeleteLostProperty", "id为空", errors.New("id为空"))
		return context.RetError(c, http.StatusBadRequest, http.StatusBadRequest, "id为空")
	}
	ResData := LostPropertyResDataParam{}
	for _, j := range LostProperty.Id {
		err := model.DeleteLostProperty(j)
		if err != nil {
			writeLostPropertyLoggerLoggerLog("DeleteLostProperty", err.Error(), err)
			ResData.Failed = append(ResData.Failed, j)
			ResData.Reason = append(ResData.Reason, err.Error())
		} else {
			ResData.Success = append(ResData.Success, j)
		}
	}
	return context.RetData(c, ResData)
}

func GetByID(c echo.Context) error {
	LostProperty := IDparam{}
	LostProperty.Id = c.QueryParam("id")
	if LostProperty.Id == "" {
		writeLostPropertyLoggerLoggerLog("GetByID", "id为空", errors.New("id为空"))
		return context.RetError(c, http.StatusBadRequest, http.StatusBadRequest, "id为空")
	}
	data, err := model.GetById(LostProperty.Id)
	if err != nil {
		writeLostPropertyLoggerLoggerLog("GetNewLostProperty", err.Error(), err)
		return context.RetError(c, http.StatusBadRequest, http.StatusBadRequest, err.Error())
	}
	return context.RetData(c, data)
}

func GetLostPropertyAll(c echo.Context) error {
	Num := GetLostPropertyAllParam{}
	err := c.Bind(&Num)
	Num.Page, err = strconv.Atoi(c.QueryParam("page"))
	Num.Number, err = strconv.Atoi(c.QueryParam("number"))
	Num.Type, err = strconv.Atoi(c.QueryParam("type"))
	if Num.Number < 1 || Num.Page < 1 {
		writeLostPropertyLoggerLoggerLog("GetLostPropertyAll", "param error", errors.New("param error"))
		return context.RetError(c, http.StatusBadRequest, http.StatusBadRequest, "param error")
	}
	data, err := model.GetLostPropertyAll(Num.Page, Num.Number, Num.Type)
	if err != nil {
		writeLostPropertyLoggerLoggerLog("GetLostPropertyAll", err.Error(), err)
		return context.RetError(c, http.StatusBadRequest, http.StatusBadRequest, err.Error())
	}
	return context.RetData(c, data)
}

func GetNewLostProperty(c echo.Context) error {
	data, err := model.GetNewLostProperty()
	if err != nil {
		writeLostPropertyLoggerLoggerLog("GetNewLostProperty", err.Error(), err)
		return context.RetError(c, http.StatusBadRequest, http.StatusBadRequest, err.Error())
	}
	return context.RetData(c, data)
}
